package com.cloud;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@SpringBootApplication
@EnableDiscoveryClient
@Controller
public class CloudServiceZookeeperApplication {

    @Autowired
    private DiscoveryClient discoveryClient;

    public static void main(String[] args) {
        SpringApplication.run(CloudServiceZookeeperApplication.class, args);
    }


    @GetMapping("getService")
    @ResponseBody
    public String getServiceUrl(){

        List<ServiceInstance> instances = discoveryClient.getInstances("cloud-service-01-zookeeper");

        for (ServiceInstance instance: instances) {
            return instance.getUri().toString();
        }
        return null;
    }

}
